package LeetCode._01_树;
/**
 * https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/
 */
public class _116_填充每个节点的下一个右侧节点指针 {
    class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;

    public Node() {}

    public Node(int _val) {
            val = _val;
        }

    public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    };

    public Node connect(Node root) {
        if(root == null) return null;
        connectTwoNode(root.left,root.right);
        return root;
    }

    //连接两个节点
    private void connectTwoNode(Node root1,Node root2){
        if(root1 == null || root2 == null) return;

        root1.next = root2;
        connectTwoNode(root1.left,root1.right);
        connectTwoNode(root2.left,root2.right);
        connectTwoNode(root1.right,root2.left);
    }
}
